package com.fayou.algorithm.str;

import java.util.ArrayList;
import java.util.Arrays;

public class StrTest {

    public static void main(String[] args) {
//        String str = "how are you";
////        System.out.println(swapStr(str));
//
//
//        String str1 = "aaaabbcw";
//        String str2 = "cbaaaabq";
//
////        System.out.println(compareStr(str1, str2));
//
//        String str3 = "i am a good boy";
//
////        System.out.println(wordCount(str3));
//
//        String str4 = "abc";
//        permute(str4, 0, str4.length() - 1);
//        boolean abc = isPalindrome("abba");

        //  System.out.println("abc:"+abc);

        String s = "(((())))";
        char[] charArray = s.toCharArray();
        int size = charArray.length;
        int left = 1;


//        System.arraycopy(charArray,left+2,charArray,left,charArray.length-(left+2));
//        charArray[--size] =null;

        String str = "()[[[]{}[]][[]][][[]])";
//        testChar(str);
        //getNum(10);
//      func(100);

    }


    private static void testAMCAt(String str) {
        char[] charArray = str.toCharArray();

    }



    static int func(int n) {

        if (n > 1) {
            n = func(n - 1) + 1;
        }
        System.out.print(n + " ");
        return n;
    }

    /**
     * 斐波那契数列
     * @param n
     * @return
     */
    private static long getNum(int n){
        long[] f = new long[n+1];
        f[1] = 1;
        f[2] = 1;

        for (int i = 3; i <= n; i++) {
            f[i] = f[i-2]+f[i-1];

        }

        for (long l : f) {
            System.out.println("##>:"+l);
        }
        return f[n];
    }

    private static void testChar(String str) {
        // 定义一个计数器，初始值为0
        int count = 0;

        // 随便给一个实例作为判断目标
//        String str = "(((+))/5)()-/()";
        char[] charArray = str.toCharArray();

        for (int i = 0; i < charArray.length; i++) {
            if (count<0){
                break;
            }
            char temp = charArray[i];
            if (temp == '('||temp == '{'||temp == '['){
                count++;
            }

            if (temp == ')'||temp == '}'||temp == ']'){
                count--;
            }

        }
//        // 开始判断
//        for (int i = 0; i < str.length(); i++) {
//            if (count < 0) {
//                break;
//            }
//            // 临时变量，遍历的目标字符串字符
//
//            String temp = str.substring(i, i + 1);
//            // 左括号，计数器加1
//            if (temp.equals("(")) {
//                count++;
//            }
//            // 右括号，计数器减1
//            if (temp.equals(")")) {
//                count--;
//            }
//
//            // 打印字符串的字符
//            System.out.print(temp + " ");
//        }

        // 换行
        System.out.println();
        if (count == 0) {
            // 匹配为yes
            System.out.println("yes");
        } else {
            // 不匹配为not
            System.out.println("not");
        }
    }

    //
    private static String swapStr(String str) {
        char[] chars = str.toCharArray();


        int left = 0;
        int right = chars.length - 1;

        swap(chars, left, right);

        int begin = 0;
        for (int i = 0; i < chars.length; i++) {
            if (chars[i] == ' ') {
                swap(chars, begin, i - 1);
                begin = i + 1;
            }
        }

        swap(chars, begin, chars.length - 1);
        return new String(chars);
    }

    private static void swap(char[] chars, int left, int right) {
        while (left < right) {
            char temp = chars[left];
            chars[left] = chars[right];
            chars[right] = temp;

            left++;
            right--;
        }
    }

    //判断连个字符串的字母个字母的个数是否一样，顺序可以不同， 如“aaaabc” 和“cbaaaa”是相同的
    public static boolean compareStr(String str1, String str2) {
        if (str1 == null || str2 == null) {
            return false;
        }

        if (str1.length() != str2.length()) {
            return false;
        }

        byte[] byte1 = str1.getBytes();
        byte[] byte2 = str2.getBytes();

        Arrays.sort(byte1);
        Arrays.sort(byte2);

        for (int i = 0; i < byte1.length; i++) {
            if (byte1[i] != byte2[i]) {
                return false;
            }
        }
        return true;
    }

    private static int wordCount(String str) {
        int word = 0;
        int count = 0;
        //i am a good boy
        for (int i = 0; i < str.length(); i++) {
            if (str.charAt(i) == ' ') {
                word = 0;
            } else if (word == 0) {
                word = 1;
                count++;
            }
        }

        return count;
    }

    private static String removeDuplicate(String str) {
        StringBuffer stringBuffer = new StringBuffer();
        int i = 0;
        int length = str.length();

        while (i < length) {
            char c = str.charAt(i);
            stringBuffer.append(c);
            i++;
            while (i < length && str.charAt(i) == c) {
                i++;
            }
        }

        return stringBuffer.toString();
    }


    private static void permute(String str, int start, int end) {
        if (start == end) {
            System.out.println("str:" + str);
        } else {
            for (int i = start; i <= end; i++) {
                str = swap(str, start, i);
                permute(str, start + 1, end);

                str = swap(str, start, i);
            }
        }
    }


    private static String swap(String a, int i, int j) {
        char temp;

        char[] charArray = a.toCharArray();
        temp = charArray[i];
        charArray[i] = charArray[j];
        charArray[j] = temp;
        return String.valueOf(charArray);
    }

    /**
     * 回文串
     *
     * @return
     */
    private static boolean isPalindrome(String str) {
        if (str == null || str.length() == 0) {
            return true;
        }

        int front = 0;
        int end = str.length() - 1;


        while (front < end) {
            while (front < str.length() && !Character.isLetterOrDigit(str.charAt(front))) {
                front++;
            }


            if (front == str.length()) {
                return true;
            }

            while (end >= 0 && !Character.isLetterOrDigit(str.charAt(end))) {
                end--;
            }


            if (Character.toLowerCase(str.charAt(front)) != Character.toLowerCase(str.charAt(end))) {
                break;
            } else {
                front++;
                end--;
            }
        }

        return end <= front;
    }
}
